/**
 * 
 */
package com.globallogic.training.university.study;

import java.util.*;

import com.globallogic.training.university.Strings;

/**
 * A <code>Course</code> object represents a course of study. It consists of
 * lectures and seminars
 * 
 * @author vasiliy.kulgaviy
 * 
 */
public class Course {
	private String id;
	private String name;
	private int grade;

	private List<Lessonable> lessons = new ArrayList<Lessonable>();
	private int currentLessonIndex = 0;

	/**
	 * Constructs a course with defined Id, Name and Grade
	 * 
	 * @param id
	 *            the id of the new person
	 * @param name
	 *            the name of the new person
	 * @param grade
	 *            the grade of the new person
	 */
	public Course(String id, String name, int grade) {
		this.id = id;
		this.name = name;
		this.grade = grade;
	}

	/**
	 * Adds a lecture or a seminar to a course
	 * 
	 * @param lesson
	 *            lesson to add
	 */
	public void addLesson(Lessonable lesson) {
		lessons.add(lesson);
	}

	/**
	 * Refreshes index of current lesson to zero. It's used to make a schedule
	 * of a teacher
	 */
	public void refreshCurrentLessonIndex() {
		currentLessonIndex = 0;
	}

	/**
	 * Increments index of current lesson. It's used to make a schedule of a
	 * teacher
	 */
	public void incrementCurrenLessonIndex() {
		currentLessonIndex++;
	}

	/**
	 * Returns current lesson. It's used to make a schedule of a teacher
	 * 
	 * @return current lesson. If the end of the lessons is reached it returns
	 *         null
	 */
	public Lessonable getCurrentLesson() {
		if (currentLessonIndex < lessons.size()) {
			return lessons.get(currentLessonIndex);
		} else {
			return null;
		}
	}

	/**
	 * Returns the id of the course
	 * 
	 * @return the id of the course
	 */
	public String getId() {
		return id;
	}

	/**
	 * Returns the name of the course
	 * 
	 * @return the name of the course
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the grade of the course
	 * 
	 * @return the grade
	 */
	public int getGrade() {
		return grade;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (other == null) return false;
		
		if (this == other)
			return true;

		if (!(other instanceof Course))
			return false;

		Course otherCourse = (Course) other;

		return id.equals(otherCourse.id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 17;
		int result = 37;

		result = prime * result + id.hashCode();

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String.format(Strings.TO_STRING_COURSE, id, name);
	}
}
